package com.pure.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

/**
 * @description:
 * @date: 2022-06-24 10:27
 * @since 1.2.0
 **/
@Slf4j
public class PureBigDecimalUtil {

    private static Integer ZERO = 0;
    private static Double DOUBLE_ZERO = 0D;
    private static Double SCALE_FACTOR = 0.000001;

    public static void main(String[] args) {


        BigDecimal beforePrice = BigDecimal.valueOf(66.00000D);
        log.info("beforePrice = {}", beforePrice.toString());
        BigDecimal afterPrice = BigDecimal.valueOf(66D);
        log.info("afterPrice = {}", afterPrice.toString());
        if (beforePrice.compareTo(afterPrice) != 0) {
            log.info("Double 格式构造，小数位精度影响比值相等。。。");
        }


        BigDecimal beforeBigDecimal = new BigDecimal("664.10000");
        log.info("beforeBigDecimal = {}", beforeBigDecimal.stripTrailingZeros());
        BigDecimal afterBigDecimal = new BigDecimal("664");
        log.info("afterBigDecimal = {}", afterBigDecimal.stripTrailingZeros());
        if (beforeBigDecimal.compareTo(afterBigDecimal) != 0) {
            log.info("建议，使用字符串格式构造，不会损失精度，小数位精度影响比值相等。。。");
        }


        BigDecimal minuend = BigDecimal.valueOf(66.00);
        BigDecimal subtrahend = BigDecimal.valueOf(68.78);

        if (minuend.subtract(subtrahend).compareTo(BigDecimal.ZERO) == -1) {
            log.info("发货类型为高开时，价格需要 > {}", subtrahend);
        }

    }

    private static void bigDecimalMethod() {
        String annulUnit = "2001";
//        String terminal = "账期30天";
        String terminal = "";
        BigDecimal bigDecimal = new BigDecimal(66.31000D);
        double v = bigDecimal.doubleValue();

        Long materialNo = 1529741022704603136L;

        StringBuffer keyBuffer = new StringBuffer();
        String key = keyBuffer.append(annulUnit).append(materialNo).append(terminal).toString();
        log.info("key = {}", key);


        BigDecimal zero = BigDecimal.ZERO;
        if (zero.doubleValue() == ZERO) {
            log.info("BigDecimal double value = {}", zero.doubleValue());
        }

        Double zeroD = new BigDecimal(0.000001).doubleValue();

        if (zeroD.compareTo(DOUBLE_ZERO) <= ZERO) {
            log.info("< 0 || == 0");
        }


        Double abs = Math.abs(zeroD);
        log.info("abs = {}", abs.toString());
        if (abs < SCALE_FACTOR) { // 保留5位小数
            if (zeroD.equals(DOUBLE_ZERO)) {
                log.info("zeroD == DOUBLE_ZERO double value = {}", zeroD.toString());
            }
        } else {
            if (zeroD.equals(DOUBLE_ZERO)) {
                log.info("zeroD == DOUBLE_ZERO double value = {}", zeroD.toString());
            } else {
                log.info("abs < SCALE_FACTOR double value = {}", zeroD.toString());
            }
        }
    }
}
